package com.qinhai.webcs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinhai.webcs.mapper.IntegralsMapper;
import com.qinhai.webcs.pojo.Integrals;
import com.qinhai.webcs.pojo.Product;
import com.qinhai.webcs.pojo.User;
import com.qinhai.webcs.service.CartService;
import com.qinhai.webcs.service.ProductCategoryService;
import com.qinhai.webcs.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 秦海
 * @since 2021-04-11
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private CartService cartService;
    @Autowired
    private IntegralsMapper integralsMapper;

    //跳转商品的列表
    @GetMapping("/productList/{type}/{id}")
    public ModelAndView getProductList(
            @PathVariable("type") String type,
            @PathVariable("id") Integer id,
            HttpSession session
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        modelAndView.addObject("productList", productService.findByCategoryId(id, type));
        modelAndView.addObject("list", productCategoryService.getAllProductCategoryVO());
        User user = (User) session.getAttribute("user");
        if (user != null) {
            modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    //跳转到商品详情页
    @GetMapping("/productOne/{id}")
    public ModelAndView findById(@PathVariable("id") Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productDetail");
        modelAndView.addObject("product", productService.getById(id));
        modelAndView.addObject("list", productCategoryService.getAllProductCategoryVO());

        User user = (User) session.getAttribute("user");
        if (user != null) {
            modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    //搜索商品
    @PostMapping("/searchProduct")
    private ModelAndView searchProduct(@RequestParam("productName") String productName, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", productName);
        List<Product> list = productService.list(queryWrapper);
        modelAndView.addObject("productList", list);
        modelAndView.addObject("list", productCategoryService.getAllProductCategoryVO());
        User user = (User) session.getAttribute("user");
        if (user != null) {
            modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }
        modelAndView.setViewName("productList");
        return modelAndView;
    }

    @GetMapping("/integralProductList")
    public ModelAndView getIntegralProductList(
            HttpSession session
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("integralProductListPage");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("categorylevelone_id", 0);
        List<Product> productList = productService.list(queryWrapper);
        modelAndView.addObject("productList", productList);
        modelAndView.addObject("list", productCategoryService.getAllProductCategoryVO());
        User user = (User) session.getAttribute("user");
        if (user != null) {
            modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    @GetMapping("/integralOne/{id}")
    public ModelAndView findIntegralById(@PathVariable("id") Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("integralProductDetail");
        modelAndView.addObject("product", productService.getById(id));
        modelAndView.addObject("list", productCategoryService.getAllProductCategoryVO());

        User user = (User) session.getAttribute("user");

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getId());
        Integrals integrals = integralsMapper.selectOne(queryWrapper);

        Integer totalIntegrals;

        if (integrals == null) {
            totalIntegrals = 0;
        } else {
            totalIntegrals = integrals.getIntegral();
        }

        if (user != null) {
            modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }

        modelAndView.addObject("totalIntegrals", totalIntegrals);
        return modelAndView;
    }
}

